var url = document.URL; //页面导航栏地址
var token = getCookie("signature");
buildid = getParams("buildid");

setTimeout(function() {
  if (token) {
    $("#logind").html("个人中心");
  } else {
    $("#logind").html("登录");
  }
}, 200);

function goLoginOr() {
  setTimeout(function() {
    if (token) {
      window.location.href = "/console/#/Home/Index";
    } else {
      window.location.href = "/console/#/Login";
    }
  }, 200);
}


//获取浏览量量
ajax(
  "/api/v1/user/opt/countVisit",
  "post",
  { id: buildid, type: "1" },
  function(res) {
    $("#browseNum").html(res.data);
  }
);
//获取关注数量
ajax(
  "/api/v1/user/opt/attentionTypeNum",
  "post",
  { id: buildid, type: "1" },
  function(res) {
    $("#focusNum").html(res.data);
  }
);
//获取用户是否关注此楼盘
function focus() {
  if (token != "null") {
    ajax(
      "/api/v1/user/opt/checkAttention",
      "post",
      { id: buildid, type: "1" },
      function(res) {
        if (res.data == 1) {
          $("#guanzhu_box2").show();
          $("#guanzhu_box1").hide();
        }
      }
    );
  }
}
focus();
//点击关注
function addFocus() {
  if (token) {
    ajax(
      "/api/v1/user/opt/addAttention",
      "post",
      { id: buildid, attentionType: "1" },
      function(res) {
        focus();
      }
    );
  } else {
    window.location.href = "/item/views/login/login.html";
  }
}
//点击周边推荐进入详情
function goToBuildingDetails(name, id) {
  window.location.href =
        "/three/main.html?name=" + name + "&cnt=0&buildid=" + id;
}
//跳转房源搜索页
function goToBuildingHouse() {
  var keyword = document.getElementById("keyword").value;
  window.location.href =
    "/item/views/search/searchBuilding.html?keyword=" + keyword;
}
//回车搜索
function getkey(e) {
  if (e.keyCode == 13) {
    goToBuildingHouse();
  }
}
function fyy(query) {
  $("#page").empty();
  $("#listfjh").empty();
  query.buildingId = buildid;
  query.page = "1";
  query.size = "8";
  dzkj1(query);
  var number;
  var num1 = resu1.data.count % 8;
  if (num1 == 0) {
    number = resu1.data.count / 8;
  } else {
    number = parseInt(resu1.data.count / 8) + 1;
  }
  //分页
  $("#page").paging({
    pageNo: 8,
    totalPage: number,
    callback: function(num) {
      $("#listfjh").empty();
      query.page = num;
      query.buildingId = buildid;
      query.size = "8";
      dzkj1(query);
    }
  });
}
//待租空间
function dzkj1(query) {
  ajax(
    "/api/v1/building/buildingHomeRentSpace",
    "post",
    // { buildingId: buildid, page: page1, size: "8" },
    query,
    function(result) {
      resu1 = result;
      if (result.code == 200) {
        roomNo = [];
        $("#dzkjzs").html("待租空间(" + resu1.data.count + "个)");
        for (var i = 0; i < result.data.list.length; i++) {
          var name = result.data.list[i].houseName.replace(buildingName+" |",'');          

          roomNo.push(result.data.list[i].roomNo);
          var temp = result.data.list[i].releaseTime;
          if (temp != null) {
            var time = temp.substring(0, temp.length - 9);
          } else {
            var time = "";
          }
          if (result.data.list[i].imgUrl) {
            var area = result.data.list[i].area;
            $("#listfjh").append(
              '<div id="' +
                result.data.list[i].roomNo +
                '" onclick="fyxq(' +
                result.data.list[i].houseId +
                ',this);" onmouseover="movein(this)" onmouseout="moveout(this)" class="list-model" >' +
                '<img src="' +
                getImgUrl() +
                result.data.list[i].imgUrl +
                '">' +
                '<div class="info">' +
                '<h4 class="ellipsis-1">' +
                name +
                "</h4>" +
                '<p><span class="big">' +
                result.data.list[i].price +
                "</span>元/m²·天</p>" +
                '<p class="clearfix">' +
                '<small class="pull-left">' +
                (result.data.list[i].price*30*area/10000).toFixed(1)  +
                "万元/月</small>" +
                '<small class="pull-right">' +
                time +
                "</small>" +
                "</p>" +
                "</div>" +
                "</div>"
            );
          } else {
            $("#listfjh").append(
              '<div id="' +
                result.data.list[i].roomNo +
                '" onclick="fyxq(' +
                result.data.list[i].houseId +
                ',this);" onmouseover="movein(this)" onmouseout="moveout(this)" class="list-model" >' +
                '<img src="static/ffyxq/images/noupload.png">' +
                '<div class="info">' +
                '<h4 class="ellipsis-1">' +
                name +
                "</h4>" +
                '<p><span class="big">' +
                result.data.list[i].price +
                "</span>元/m²·天</p>" +
                '<p class="clearfix">' +
                '<small class="pull-left">' +
               (result.data.list[i].price*30*area/10000).toFixed(1)  +
                "万元/月</small>" +
                '<small class="pull-right">' +
                time +
                "</small>" +
                "</p>" +
                "</div>" +
                "</div>"
            );
          }
          //onclick="fyxq('+result.data[i].houseId+');"
        }
      }
      if (dzc_service != undefined) {
        gethousehold();
      }
    },
    false
  );
}

//待租空间按最新排序
zxSortNum = 1;
query = {
  new: "0",
  floor: "-1",
  area: "-1",
  price: "-1"
};

function zxSort(num) {
  query.floor = "-1";
  query.area = "-1";
  query.price = "-1";
  lcSortNum = 0;
  mjSortNum = 0;
  yzSortNum = 0;
  $("#zx")
    .parent()
    .siblings()
    .removeClass("active");
  $("#zx")
    .parent()
    .siblings()
    .find(".fa-long-arrow-up")
    .hide();
  $("#zx")
    .parent()
    .siblings()
    .find(".fa-long-arrow-down")
    .hide();
  if (num == zxSortNum) {
    $("#zx")
      .parent()
      .removeClass("active");
    query.new = String(zxSortNum);
    fyy(query);
    zxSortNum = 1;
  } else {
    $("#zx")
      .parent()
      .addClass("active");
    query.new = String(zxSortNum);
    fyy(query);
    zxSortNum = 0;
  }
}
//待租空间按楼层排序
lcSortNum = 0;

function lcSort(num) {
  query.new = "0";
  query.area = "-1";
  query.price = "-1";
  zxSortNum = 1;
  mjSortNum = 0;
  yzSortNum = 0;
  $("#lc")
    .parent()
    .siblings()
    .removeClass("active");
  $("#lc")
    .parent()
    .siblings()
    .find(".fa-long-arrow-up")
    .hide();
  $("#lc")
    .parent()
    .siblings()
    .find(".fa-long-arrow-down")
    .hide();
  if (num == lcSortNum) {
    $("#lc .fa-long-arrow-up").show();
    $("#lc .fa-long-arrow-down").hide();
    $("#lc")
      .parent()
      .addClass("active");
    query.floor = String(lcSortNum);
    fyy(query);
    lcSortNum = 1;
  } else if (lcSortNum == 1) {
    $("#lc .fa-long-arrow-up").hide();
    $("#lc .fa-long-arrow-down").show();
    $("#lc")
      .parent()
      .addClass("active");
    query.floor = String(lcSortNum);
    fyy(query);
    lcSortNum = 2;
  } else {
    $("#lc .fa-long-arrow-up").hide();
    $("#lc .fa-long-arrow-down").hide();
    $("#lc")
      .parent()
      .removeClass("active");
    query.floor = String(lcSortNum);
    fyy(query);
    lcSortNum = 0;
  }
}
//待租空间按面积排序
mjSortNum = 0;

function mjSort(num) {
  query.floor = "-1";
  query.new = "0";
  query.price = "-1";
  lcSortNum = 0;
  zxSortNum = 1;
  yzSortNum = 0;
  $("#mj")
    .parent()
    .siblings()
    .removeClass("active");
  $("#mj")
    .parent()
    .siblings()
    .find(".fa-long-arrow-up")
    .hide();
  $("#mj")
    .parent()
    .siblings()
    .find(".fa-long-arrow-down")
    .hide();
  if (num == mjSortNum) {
    $("#mj .fa-long-arrow-up").show();
    $("#mj .fa-long-arrow-down").hide();
    $("#mj")
      .parent()
      .addClass("active");
    query.area = String(mjSortNum);
    fyy(query);
    mjSortNum = 1;
  } else if (mjSortNum == 1) {
    $("#mj .fa-long-arrow-up").hide();
    $("#mj .fa-long-arrow-down").show();
    $("#mj")
      .parent()
      .addClass("active");
    query.area = String(mjSortNum);
    fyy(query);
    mjSortNum = 2;
  } else {
    $("#mj .fa-long-arrow-up").hide();
    $("#mj .fa-long-arrow-down").hide();
    $("#mj")
      .parent()
      .removeClass("active");
    query.area = String(mjSortNum);
    fyy(query);
    mjSortNum = 0;
  }
}
//待租空间按月租排序
yzSortNum = 0;

function yzSort(num) {
  query.floor = "-1";
  query.area = "-1";
  query.price = "-1";
  lcSortNum = 0;
  mjSortNum = 0;
  zxSortNum = 1;
  $("#yz")
    .parent()
    .siblings()
    .removeClass("active");
  $("#yz")
    .parent()
    .siblings()
    .find(".fa-long-arrow-up")
    .hide();
  $("#yz")
    .parent()
    .siblings()
    .find(".fa-long-arrow-down")
    .hide();
  if (num == yzSortNum) {
    $("#yz .fa-long-arrow-up").show();
    $("#yz .fa-long-arrow-down").hide();
    $("#yz")
      .parent()
      .addClass("active");
    query.price = String(yzSortNum);
    fyy(query);
    yzSortNum = 1;
  } else if (yzSortNum == 1) {
    $("#yz .fa-long-arrow-up").hide();
    $("#yz .fa-long-arrow-down").show();
    $("#yz")
      .parent()
      .addClass("active");
    query.price = String(yzSortNum);
    fyy(query);
    yzSortNum = 2;
  } else {
    $("#yz .fa-long-arrow-up").hide();
    $("#yz .fa-long-arrow-down").hide();
    $("#yz")
      .parent()
      .removeClass("active");
    query.price = String(yzSortNum);
    fyy(query);
    yzSortNum = 0;
  }
}
//添加浏览记录
function addVisit() {
  // if (localStorage.getItem("token")) {
  var $this = this;
  ajax(
    "/api/v1/user/opt/addVisit",
    "POST",
    {
      id: getParams("buildid"),
      type: "1"
    },
    function(res) {}
  );
}
addVisit();
